package com.scsj.supermarket.view.fragment.myordermodel;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.OrderAdapter;
import com.scsj.supermarket.base.QueryMineOrderBase;
import com.scsj.supermarket.bean.MineOrderBean;
import com.scsj.supermarket.bean.MineOrderRequestBean;
import com.scsj.supermarket.presenter.MineOrderPresenter;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;

/**
 * 全部订单
 */
public class TotalOrderFragment extends BaseFragment implements QueryMineOrderBase.IView{

    private MineOrderPresenter presenter;
    private OrderAdapter orderAdapter;
    private RecyclerView all_order_rv;
    private int i;
    private SwipeRefreshLayout swipeRefreshLayout;
    public TotalOrderFragment() {
    }
    @SuppressLint("ValidFragment")
    public TotalOrderFragment(int i) {
        this.i = i;
    }
    //我们需要创建一个整体的集合来存放所有获取到的数据
    private List<MineOrderBean.DataBean.ListBean> dataBeanList= new ArrayList<>();
    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        presenter = new MineOrderPresenter(this);
        all_order_rv = view.findViewById(R.id.all_order_rv);
        all_order_rv.setLayoutManager(new LinearLayoutManager(getHoldingActivity()));
        all_order_rv.setNestedScrollingEnabled(false);

        swipeRefreshLayout = view.findViewById(R.id.mine_order_refresh_layout);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
    }

    @Override
    protected void initListener() {
        //下拉刷新监听
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
    }
    @Override
    protected void initData() {
        getData();
    }
    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_total_order, null);
        initView(view, null);
        initListener();
        initData();
        return view;
    }

    @Override
    public void showSuccessMsg(String s, MineOrderBean mineOrderBean) {
        //数据获取成功则停止刷新
        swipeRefreshLayout.setRefreshing(false);
        if (mineOrderBean!=null&&mineOrderBean.isSuccess()){
            //此处表示数据获取成功
            dataBeanList= mineOrderBean.getData().getList();
            Toasty.info(getActivity(),dataBeanList.size()+"条数据").show();
            //将我们获取到的集合传递到Adapter中去
            orderAdapter = new OrderAdapter(dataBeanList, getHoldingActivity(),i);
            all_order_rv.setAdapter(orderAdapter);
            //这是Adapter自带的条目点击事件
            orderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                }
            });
        }
    }

    @Override
    public void showErrorMsg(String s) {

    }

    public void getData() {
        /**
         * 这是一步整体的网络操作请求
         */
        MineOrderRequestBean.PageInfoBean bean = new MineOrderRequestBean.PageInfoBean();
        bean.setPageNum("1");
        bean.setPageSize("10");
        //这个i的值表示获取的订单列表类型：0.全部 1.待付款 2.待发货 3.待收货 4.待评价 5.已退款
        MineOrderRequestBean requestBean = new MineOrderRequestBean(String.valueOf(i),bean);
        Gson gson = new Gson();
        String obj = gson.toJson(requestBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), obj);
        presenter.deliverData(body);
        /**
         * 这是一步整体的网络操作请求
         */
    }

    @Override
    public void onPause() {
        super.onPause();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        swipeRefreshLayout.setRefreshing(false);
    }
}
